package com.core.dao;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.core.base.dao.BaseHibernateDao;
import com.core.model.Department;

/**
 * 部门DAO
 * 
 * @author pt-chenqw
 *
 */
@Repository("departmentDAO")
public class DepartmentDAO extends BaseHibernateDao<Department> {
	
	/**
	 * 查询所有的部门列表    
	 * @return
	 */
	public List<Department> getDepartments(){
		return findAll("from Department where status = 1");
	}
	
	/**
	 * 根据ID查询部门信息
	 * @param id
	 * @return
	 */
	public Department getDepartmentByID(Integer id) {
		String hql = "from Department where id = ?";
		return findOne(hql, id);
	}

	/**
	 * 更新部门信息
	 * @param department
	 */
	public void updateDepartment(Department department) {
		Department dept = getDepartmentByID(department.getId());
		dept.setRemark(department.getRemark());
		dept.setStatus(department.getStatus());
		dept.setUpdatetime(new Date());
		update(dept);
	}
	
	/**
	 * 批量删除部门
	 * @param ids
	 */
	public void deleteDepartmentsByIDS(String ids){
		List<Department> departments = find("from Department where id in (?)", ids);
		deleteBatch(departments);
	}

	/**
	 * 删除部门信息
	 * @param id
	 */
	public void deleteDepartmentByID(Integer id) {
		Department department = getDepartmentByID(id);
		delete(department);
	}

	/**
	 * 添加部门信息
	 * @param department
	 */
	public void addDepartment(Department department) {
		department.setCreatetime(new Date());
		department.setUpdatetime(new Date());
		save(department);
	}
	
	/***
	 * 根据code查询
	 * @param deptCode
	 * @return
	 */
	public Department getDepartmentByCode(String deptCode){
		String hql = "from Department where deptcode = ?";
		return findOne(hql, deptCode);
	}
	
	

}
